<?php
require_once MDL_TABLES;
$tables = getTables();

if (isset($_SESSION[KEY_SESSION_USER])) {
    require_once CLASS_USER;
    $user = unserialize($_SESSION[KEY_SESSION_USER]);
    $logged = $user->isRegistered();
} else {
    $user = null;
    $logged = false;
}

$db = openConnection();

if ($user) {
    $supervised = $db->select(array('supervisiona', 'user', 'studente'), array('IDstudente', 'studente.cognome', 'studente.nome'), 'supervisiona.IDuser=user.ID AND supervisiona.IDstudente=studente.ID AND user.ID=' . $user->get ('ID'));
} else {
    $supervised = null;
}
?>
<div id="menu_div">
    <hr />
    <table cellspacing="0" id="menu">
        <tr>
            <td <?php if (isset($request) && $request == 'delete') echo 'id="active"'; ?>>
                <a href="<?php echo HTTP_CTRL_MAIN . '?request=delete'; ?>"><strong>Elimina</strong></a>
                <table cellspacing="0">
                    <?php foreach ($tables as $key => $tab) : if ($logged && $user->can('delete', $key)) : ?>
                            <tr>
                                <td><a href="<?php echo HTTP_CTRL_MAIN . "?request=delete&table=$key"; ?>"><?php echo $tab; ?></a></td>
                            </tr>
                            <?php
                        endif;
                    endforeach;
                    ?>
                </table>
            </td>
            <td <?php if (isset($request) && $request == 'insert') echo 'id="active"'; ?>>
                <a href="<?php echo HTTP_CTRL_MAIN . '?request=insert'; ?>"><strong>Inserisci</strong></a>
                <table cellspacing="0">
                    <?php foreach ($tables as $key => $tab) : if ($logged && $user->can('insert', $key)) : ?>
                            <tr>
                                <td><a href="<?php echo HTTP_CTRL_MAIN . "?request=insert&table=$key"; ?>"><?php echo $tab; ?></a></td>
                            </tr>
                            <?php
                        endif;
                    endforeach;
                    ?>
                </table>
            </td>
            <td <?php if (isset($request) && $request == 'edit') echo 'id="active"'; ?>>
                <a href="<?php echo HTTP_CTRL_MAIN . '?request=edit'; ?>"><strong>Modifica</strong></a>
                <table cellspacing="0">
                    <?php foreach ($tables as $key => $tab) : if ($key == 'permesso' || $key == 'supervisiona') continue; if ($logged && $user->can('edit', $key)) : ?>
                            <tr>
                                <td><a href="<?php echo HTTP_CTRL_MAIN . "?request=edit&table=$key"; ?>"><?php echo $tab; ?></a></td>
                            </tr>
                            <?php
                        endif;
                    endforeach;
                    ?>
                </table>
            </td>
            <td <?php if (isset($request) && $request == 'view') echo 'id="active"'; ?>>
                <a href="<?php echo HTTP_CTRL_MAIN . '?request=view'; ?>"><strong>Visualizza</strong></a>
                <table cellspacing="0">
                    <?php foreach ($tables as $key => $tab) : if ($logged && $user->can('view', $key)) : ?>
                            <tr>
                                <td><a href="<?php echo HTTP_CTRL_MAIN . "?request=view&table=$key"; ?>"><?php echo $tab; ?></a></td>
                            </tr>
                            <?php
                        endif;
                    endforeach;
                    ?>
                </table>
            </td>
            <td <?php if (isset($stat)) echo 'id="active"'; ?>>
                <a href="<?php echo HTTP_CTRL_MAIN . '?stat='; ?>"><strong>Statistiche</strong></a>
                <table cellspacing="0">
                    <?php foreach ($tables as $key => $tab) : if ($user && $user->can('supervise', $tab)) : ?>
                            <tr>
                                <td><a href="<?php echo HTTP_CTRL_MAIN . "?stat=$key"; ?>"><?php echo $tab; ?></a></td>
                            </tr>
                        <?php
                        endif;
                    endforeach;
                    ?>
                </table>
            </td>
<?php if ($supervised) : ?>
                <td>
                    <a href="#"><strong>Supervisiona</strong></a>
                    <table cellspacing="0">
    <?php while ($row = $db->fetchAssoc($supervised)) : ?>
                            <tr>
                                <td><a href="<?php echo HTTP_CTRL_MAIN . '?ID=' . $row['IDstudente']; ?>&stat=studente"><?php echo $row['cognome'] . ' ' . $row['nome']; ?></a></td>
                            </tr>
    <?php endwhile; ?>
                    </table>
                </td>
<?php endif; ?>
            <td <?php if (isset($account)) echo 'id="active"'; ?>>
                <a href="#"><strong>Altro</strong></a>
                <table cellspacing="0">
                    <tr>
<?php if ($user && $user->can('insert', 'permesso')) : ?>
                        <tr>
                            <td><a href="<?php echo HTTP_CTRL_MAIN; ?>?account=generate_permissions">Genera Permessi</a></td>
                        </tr>
                        <tr>
                            <td><a href="<?php echo HTTP_CTRL_MAIN; ?>?account=pg_permissions" title="">Modelli</a></td>
                        </tr>
                        <tr>
                            <td><a href="<?php echo HTTP_CTRL_MAIN; ?>?account=backup" title="">Backup</a></td>
                        </tr>
                        <tr>
                            <td><a href="<?php echo HTTP_CTRL_MAIN; ?>?account=find" title="">Trova interventi</a></td>
                        </tr>
<?php endif; ?>
                </table>
            </td>

            <?php
            if ($logged) :
                ?>
                <td><a href="<?php echo HTTP_CTRL_MAIN; ?>?account=logout">Logout</a></td>
                <?php
            endif;
            if (!$logged) :
                ?>
                <td><a href="<?php echo HTTP_CTRL_MAIN; ?>?account=login">Login</a></td>
<?php endif; ?>
        </tr>
    </table>
</div>